<?php

include( "./admin/include/func.php");
include( "./admin/include/thumb.php");

$panels = array ();

$g_id  = isset( $_GET['sorszam']) ? (int) $_GET['sorszam'] : 0;
$g_kod = isset( $_GET['kod']) ? trim( strip_tags( $_GET['kod'])) : '';

$db->sql = "SELECT kep,nev,email,telefon,targy,szoveg,rovat,ar,telepules,regio,weblap,feladas 
            FROM hirdetes 
            WHERE id='$g_id' AND aktivkod='$g_kod' AND aktiv=1";
$db->query();	
//print $db->sql;

if (mysql_num_rows( $db->rs) < 1) {
	
	$error_panel = array (
		'caption' => 'Hiba',
		'title'   => 'Hiba!',
		'body'    => "<ul class='errors'>Nem létező vagy nem aktív hirdetés!</ul>",
		'height'  => 20
	);
	
	array_unshift( $panels, $error_panel);
		
} else {

	$rs = mysql_fetch_assoc( $db->rs);

	$p_nev       = $rs['nev'];
	$p_email     = $rs['email'];
	$p_telefon   = $rs['telefon'];
	$p_targy     = $rs['targy'];
	$p_szoveg    = $rs['szoveg'];
	$p_rovat     = $rs['rovat'];
	$p_ar        = $rs['ar'];
	$p_telepules = $rs['telepules'];
	$p_regio     = $rs['regio'];
	$p_weblap    = $rs['weblap'];

	if ($rs['kep'] != '') {
		$kep   = $upload_path . '/' . str_replace('-','/',$rs['feladas']) . '/kep/' . $rs['kep']; 
		$thumb = $upload_path . '/' . str_replace('-','/',$rs['feladas']) . '/thumb/thumb_' . $rs['kep'];
		if (! file_exists ($kep)) {
			$kep   = 'http://hirdetek.net/kepek/'.$rs['kep'];
			$thumb = 'http://hirdetek.net/thumbs1/thumb_'.$rs['kep'];
		}
	} else {
		$kep   = '#';
		$thumb = './images1/nincskep.gif'; 
	}
	
	$panel = array (
		'caption' => 'Hirdetés módosítás',
		'title'   => 'Módosíthatja a hirdetés adatait!',
		'body'    => '',
		'height'  => '870'
	);

	array_push( $panels, $panel);

	if (isset($_POST['modositas'])) {

		$p_keptorles = isset ($_POST['keptorles']);		
		$p_nev       = trim(strip_tags($_POST['nev']));
		$p_telefon   = trim(strip_tags($_POST['telefon']));
		$p_targy     = trim(strip_tags($_POST['targy']));
		$p_szoveg    = strip_tags($_POST['szoveg']);
		$p_kep       = @$_FILES['kep'];
		$p_rovat     = (int)$_POST['rovat'];
		$p_ar        = trim(strip_tags($_POST['ar']));
		$p_telepules = trim(strip_tags($_POST['telepules']));
		$p_regio     = (int)$_POST['regio'];
		$p_weblap    = trim(strip_tags($_POST['weblap']));

		$success = true;
		$errors = array();
		
		if ($p_nev == '') {
			$success = false;
			array_push($errors, "A Név nincs megadva!");
		}

		if ($p_targy == '') {
			$success = false;
			array_push($errors, "A Tárgy nincs megadva!");
		}   
	   
		if (($p_szoveg) != '' && !preg_match('/^[\s\S]{0,500}$/u', $p_szoveg)) {
			$success = false;
			array_push($errors, "A Szöveg max 500 karakter lehet!");
		}

		if ($p_szoveg == '') {
			$success = false;
			array_push($errors, "A Szöveg nincs megadva!");
		} 
	   
		if (($p_rovat) != '' && !preg_match('/^[0-9]{0,10}$/', $p_rovat)) {
			$success = false;
			array_push($errors, "Hibás Rovat!");
		}
		
		if ($p_rovat < 1) {
			$success = false;
			array_push($errors, "A Rovat nincs megadva!");
		}
		   
		if (($p_regio) != '' && !preg_match('/^[0-9]{0,10}$/', $p_regio)) {
			$success = false;
			array_push($errors, "Hibás Régió!");
		}
		
		if ($p_regio < 1) {
			$success = false;
			array_push($errors, "A Régió nincs megadva!");
		}
	  
		if ('' != $p_weblap && 0 !== strpos ($p_weblap, 'http://')) $p_weblap = 'http://' . $p_weblap;
		  
		if (($p_weblap) != '' && !preg_match('/^((http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}((:[0-9]{1,5})?\/.*)?$/i', $p_weblap)) {
			$success = false;
			array_push($errors, "Hibás Weblap!");
		}
		
		if (isset ($p_kep) && isset ($p_kep['name']) && $p_kep['name'] != '') {
			
			if ($p_kep['size'] === 0) {
				$success = false;
				array_push($errors, "Hibás fájl!");
			}
			
			if ($p_kep['size'] > 512000) {
				$success = false;
				array_push($errors, "Túlméretes fájl!");
			}
			
			if ($p_kep['error'] != UPLOAD_ERR_OK) {
				$success = false;
				array_push($errors, "Fájl feltöltési hiba!");
			}
			
			if (!is_uploaded_file($p_kep['tmp_name'])) {
				$success = false;
				array_push($errors, "Fájl feltöltési hiba!");
			}
			
			//Check file mime type
			/*
			not working on hostgator
			$finfo = finfo_open(FILEINFO_MIME);
			$mime = finfo_file($finfo, $p_kep['tmp_name']);
			finfo_close($finfo);
			if (!in_array(mime_content_type($p_kep['tmp_name']), array('image/jpeg'))) {
				$success = false;
				array_push($errors, "Nem megengedett fájl típus!");
			}
			*/
			
			//print_r ($p_kep);
			
			if ($p_kep['type'] != 'image/jpeg') {
				$success = false;
				array_push($errors, "A kép nem jpg formátumú!");
			}
			
			//Sanitize the filename
			$n_kep = iconv( "UTF-8", "ASCII//TRANSLIT", $p_kep['name']);
			$n_kep = preg_replace( "/[^[:alnum:]._-]/", '_', $n_kep);

			$n_kep = time() . '-' . $n_kep; //make the filename unique

			$n_kep = strtolower ($n_kep); //make the filename and extension lowercase
			
			$n_kep_path = date ('Y', time ()) . '/' . date ('m', time ()) . '/' . date ('d', time ()) . '/kep';
			
			if (! file_exists ($upload_path . '/' . $n_kep_path)) mkdir ($upload_path . '/' . $n_kep_path, 0777, true);

			if (! move_uploaded_file ($p_kep['tmp_name'], $upload_path . '/' . $n_kep_path . '/' . $n_kep)) {
				$success = false;
				array_push($errors, "Fájl feltöltési hiba!");
			}

			//create thumb
			$thumb_path = $upload_path . '/' . date ('Y', time ()) . '/' . date ('m', time ()) . '/' . date ('d', time ()) . '/thumb';		 
			
			if (! file_exists ($thumb_path)) mkdir ($thumb_path, 0777, true);
			
			$thumb = $thumb_path . '/thumb_' . $n_kep;
			
			$kep   = $upload_path . '/' . $n_kep_path . '/' . $n_kep;
			
			if (! file_exists( $thumb)) {			
				if (create_thumb( $kep, $thumb, $twidth, $theight) === false) {
					print "Sikertelen thumb letrehozas: " . $n_kep . __FILE__ . __LINE__;
				}
			}
			
			$p_kep = $n_kep;
		
		} else $p_kep = '';
						  
		if (! $success) {
			
			$body    = "<ul class='errors'>";
			foreach ($errors as $err) $body .= "<li>$err</li>";
			$body   .= "</ul>";
			
			$error_panel = array (
				'caption' => 'Hiba',
				'title'   => 'Kérjük javítsa ki az alábbi hibákat!',
				'body'    => $body,
				'height'  => count( $errors) * 25 + 10
			);
			
			array_unshift( $panels, $error_panel);
			
		} else {
			
			$s_kep = '';
			if ($p_kep != '') $s_kep = "kep='$p_kep',";
			else if ($p_keptorles > 0)  $s_kep = "kep='',";
			
			$db->sql = "UPDATE hirdetes 
						SET nev='$p_nev',email='$p_email',telefon='$p_telefon',targy='$p_targy',szoveg='$p_szoveg',$s_kep
						rovat='$p_rovat',ar='$p_ar',telepules='$p_telepules',regio='$p_regio',weblap='$p_weblap',lastmodified=NOW() 
						WHERE id='$g_id' AND aktivkod='$g_kod'";					   
			$db->query();
			//print $db->sql;
			
			$panels = array ();
			
			$panel = array (
				'caption' => 'Információ',
				'title'   => 'Sikeres módosítás!',
				'body'    => "<p class='success'>Hirdetését sikeresen módosította!<br /><a href='javascript:history.back()'>Vissza</a> &nbsp; | &nbsp; <a href='hirdetesek.php?sorszam=$g_id'>Hirdetés megtekintése</a></p>",
				'height'  => '50'
			);
			
			array_push( $panels, $panel);
		}
	}
}

?>

<?php include ("header.php"); ?>

<?php include ("top.php"); ?>

<?php include ("left.php"); ?>
							
<div id="middle">

<?php foreach( $panels as $panel) { ?>
	
	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<p class="hpcaption"><?php print $panel['caption']; ?></p>
				<p class="hptitle"><?php print $panel['title']; ?></p>
			</div>	
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="<?php print $panel['height']; ?>px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:<?php print $panel['height']; ?>px">
				<?php if ($panel['body'] != '') print $panel['body']; else { ?>
					
					<form name="form_hirdetes" id="form_hirdetes" method="post" enctype='multipart/form-data' accept-charset="utf-8">										

						<label for='jelkep'>Jelenlegi kép</label>
						<a class='hppic' href="<?php print $kep ?>">
							<img src="<?php print $thumb; ?>" />
						</a>
						<br />
						
						<label for='keptorles'>&nbsp;</label>
						<input name='keptorles' type='checkbox' <?php if (isset($p_keptorles)) print 'checked="true"'; ?> />
						<span>Kép törlése</span>
						<br />
																		
						<label for='nev' class="required">Név</label>
						<input name='nev' type='text' value='<?php if (isset( $p_nev)) print $p_nev; ?>' />
						<p class='nnote'>Required, A hirdetést feladó neve. Max 50 karakter.</p>
						<br />
						
						<label for='email' class="required">Email</label>
						<input name='email' type='text' disabled='true' value='<?php print $p_email; ?>' />
						<p class='nnote'>Required, A hirdetést feladó email cí­me. Max 50 karakter.</p>
						<br />
						
						<label for='telefon'>Telefon</label>
						<input name='telefon' type='text' value='<?php if (isset( $p_telefon)) print $p_telefon; ?>' />
						<p class='nnote'>A hirdetést feladó telefonszáma. Max 20 karakter.</p>
						<br />
						
						<label for='targy' class="required">Tárgy</label>
						<input name='targy' id='targy' type='text' value='<?php if (isset($p_targy)) print $p_targy; ?>' onKeyDown="textCounter('targy',60,'targy-counter')" onKeyUp="textCounter('targy',60,'targy-counter')" />
						<div class="note"><span>A Tárgy még max </span><input id="targy-counter" type="text" value="60" /><span> karakter lehet.</span></div>
						<script>textCounter('targy',60,'targy-counter')</script>
						<br />
						
						<label for='szoveg' class="required">Szöveg</label>
						<textarea cols="39" rows="14" id='szoveg' name='szoveg' cols='39' rows='14' onKeyDown="textCounter('szoveg',500,'text-counter')" onKeyUp="textCounter('szoveg',500,'text-counter')"><?php if (isset($p_szoveg)) print $p_szoveg; ?></textarea>
						<div class="note"><span>A Szöveg még max </span><input id="text-counter" type="text" value="500" /><span> karakter lehet.</span></div>
						<script>textCounter('szoveg',500,'text-counter')</script>
						<br />

						<input name='MAX_FILE_SIZE' type='hidden' value='512000' />
						
						<label for='kep'>Kép</label>
						<input name='kep' type='file' />
						<p class='note'>max. 500 Kb 800x600 pixel jpg kép tölthetö fel</p>
						<!--br /-->

						<label for='rovat' class="required">Rovat</label>
						<select name='rovat'>  
							<?php
								if (count($rovatok) < 1) print "<option value='0'>Create a Rovat first!</option>";
								foreach ($rovatok as $rovat) {
									$selected = '';
									if (isset( $p_rovat)) {if ($p_rovat == $rovat['id']) $selected = "selected='selected'";}
									print "<option value='" . $rovat['id'] . "' $selected>" . $rovat['nev'] . "</option>";
									$alrovatok = $rovat['childs'];
									if( is_array( $alrovatok ) )
									foreach ($alrovatok as $alrovat) {
										$selected = '';
										if (isset( $p_rovat)) {if ($p_rovat == $alrovat['id']) $selected = "selected='selected'";}
										print "<option value='" . $alrovat['id'] . "' $selected>&nbsp;&nbsp;-&nbsp;" . $alrovat['nev'] . "</option>";
									}
								}
							?>                 
						</select>
						<p class='nnote'>Required, Válasszon egy rovatot a hirdetéséhez.</p>
						<br />

						<label for='ar'>Ár</label>
						<input name='ar' type='text' value='<?php if (isset($p_ar)) print $p_ar; ?>' />
						<p class='nnote'></p>
						<br />

						<label for='telepules'>Település</label>
						<input name='telepules' type='text' value='<?php if (isset( $p_telepules)) print $p_telepules; ?>' />
						<p class='nnote'>A település neve ahol Ön lakik, vagy ahonnan Érdeklödöket vár. </p>
						<br />

						<label for='regio' class="required">Régió</label>
						<select name='regio'>  
							<?php
								if (count($regiok) < 1) print "<option value='0'>Create a Régió first!</option>";
								foreach ($regiok as $regio) {
									$selected = '';
									if (isset( $p_regio)) {if ($p_regio == $regio['id']) $selected = "selected='selected'";}
									print "<option value='" . $regio['id'] . "' $selected>" . $regio['nev'] . "</option>";
									$alregiok = $regio['childs'];
									if( is_array( $alregiok ) )
									foreach ($alregiok as $alregio) {
										$selected = '';
										if (isset( $p_regio)) {if ($p_regio == $alregio['id']) $selected = "selected='selected'";}
										print "<option value='" . $alregio['id'] . "' $selected>&nbsp;&nbsp;-&nbsp;" . $alregio['nev'] . "</option>";
									}
								}
							?>                 
						</select>
						<br />

						<label for='weblap'>Weblap</label>
						<input name='weblap' type='text' value='<?php if (isset( $p_weblap)) print $p_weblap; ?>' />
						<p class='nnote'>Ha Önnek van weboldala, akkor itt megadhatja annak internetcímét. Max 50 karakter.</p>
						<br />
						
						<br />
											
						<label for="modositas">&nbsp;</label>
						<input name="modositas" type="submit" value="Módosítás"/>				

					</form>
				<?php } ?>           			
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="<?php print $panel['height']; ?>px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>

	<br />
	<br />

<?php } ?>

<br />

</div>

<?php include ("right.php"); ?>

<?php include ("footer.php"); ?>
